/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;

import model.entities.Predictions;
import model.entities.Statistics;

/**
 * 
 * @author LM.LOVE
 */
public class StatisticStationForm extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Creates new form StatisticStationForm
	 */
	public StatisticStationForm(String statisticStationID) {
		setLocation(new Point(300, 200));
		getContentPane().setBackground(Color.LIGHT_GRAY);
		setTitle("Statistic Station");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initComponents(statisticStationID);
		// Cap nhat list statistic len default list model Statistic
		doUpdateList();
		//Xac thuc cac thanh phan khoi tao
		
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 * 
	 * @param statisticID
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents

	// Ham nay dung de cap nhat lai listStatistic, listPrediction
	// khi ma them 1 phan tu moi
	// Lay du lieu tu duoi database len sau do cap nhat
	private void doUpdateList() {
		// Update list Statistic
		List<Statistics> lStatistic = statisticSH.getlStatistic();
		dlmStatistic.removeAllElements();
		for (int i = 0; i < lStatistic.size(); i++) {
			dlmStatistic.addElement("Statistic " + (i + 1));
		}
		// Update list prediction
		List<Predictions> lPrediction = statisticSH.getlPredictions();
		dlmPrediction.removeAllElements();
		for (int i = 0; i < lPrediction.size(); i++) {
			dlmPrediction.addElement("Prediction " + (i + 1));
		}

	}

	private void initComponents(String statisticStationID) {

		statisticPanel = new javax.swing.JPanel();
		statisticPanel.setBackground(new Color(102, 204, 255));
		chooseTypeSLabel = new javax.swing.JLabel();
		typeStatisticCombobox = new javax.swing.JComboBox<String>();
		typeStatisticCombobox.setFont(new Font("Tahoma", Font.BOLD, 12));
		typeStatisticCombobox.setModel(new DefaultComboBoxModel<String>(
				new String[] { "..choose type..", "Wind Speed", "Air Blast",
						"Rainfall in 24h", "Air Moisture", "Ground Moisture",
						"Air Pressure" }));
		exportTypeSlabel = new javax.swing.JLabel();
		typeSCombobox = new javax.swing.JComboBox<String>();
		typeSCombobox.setFont(new Font("Arial", Font.BOLD, 12));
		typeSCombobox.setModel(new DefaultComboBoxModel<String>(new String[] {
				"..choose type..", "Text", "Word", "Excel" }));
		statisticButton = new javax.swing.JButton();
		statisticButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mEvent) {
				if (typeStatisticCombobox.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(null,
							"Please choose statistic type!!!!");
					return;
				}
				if (typeSCombobox.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(null,
							"Please choose type of file!!!!");
					return;
				}
				// Neu thoa ca 2 dieu kien la da chon thi thong ke
				statisticSH
						.ThongKe(typeStatisticCombobox.getSelectedItem()
								.toString(), typeSCombobox.getSelectedItem()
								.toString());
				// Cap nhat lai list sau khi Statistic
				doUpdateList();
				// Set lai cho combobox type Statistic
				typeSCombobox.setSelectedIndex(0);
				// Set lai cho combobox loai can thong ke
				typeStatisticCombobox.setSelectedIndex(0);
				JOptionPane
						.showMessageDialog(null, "Statistic successfully!!!");
				JOptionPane
						.showMessageDialog(null,
								"Please check your directory to view your statistic file!!!");

			}
		});
		listStatisticPanel = new javax.swing.JPanel();
		listStatisticPanel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC,
				13));
		listStatisticPanel.setBackground(new Color(153, 153, 153));
		scrollListStatistic = new javax.swing.JScrollPane();
		listStatistic = new javax.swing.JList<String>();
		listStatistic.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mEvent) {
				// Lay noi dung cua viec thong ke ung voi Index
				String result = statisticSH.getStatisticContent(listStatistic
						.getSelectedValue());
				// Thiet lap noi dung cho text area
				statisticReportField.setText(result);
			}
		});
		scrollStatisticField = new javax.swing.JScrollPane();
		scrollStatisticField.setFont(new Font("Tahoma",
				Font.BOLD | Font.ITALIC, 13));
		statisticReportField = new javax.swing.JTextArea();
		statisticReportField.setFont(new Font("Arial Black", Font.BOLD
				| Font.ITALIC, 13));
		statisticReportField.setEditable(false);
		predictionPanel = new javax.swing.JPanel();
		predictionPanel.setBackground(new Color(0, 204, 255));
		scrollPrediction = new javax.swing.JScrollPane();
		predictionField = new javax.swing.JTextArea();
		predictionField.setWrapStyleWord(true);
		predictionField.setLineWrap(true);
		typePCombobox = new javax.swing.JComboBox<String>();
		typePCombobox.setFont(new Font("Tahoma", Font.BOLD, 13));
		typePCombobox.setModel(new DefaultComboBoxModel<String>(new String[] {
				"..choose type..", "Text", "Word", "Excel" }));
		predictButton = new javax.swing.JButton();
		predictButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mEvent) {
				if (predictionField.getText().equals("")) {
					JOptionPane.showMessageDialog(null,
							"Please predict something in field prediction!!!");
					return;
				}
				if (typePCombobox.getSelectedIndex() == 0) {
					JOptionPane.showMessageDialog(null,
							"Please choose type of file!!!!");
					return;
				}
				// Neu thoa ca 2 dieu kien la da chon thi thong ke
				statisticSH.TienDoan(predictionField.getText(), typePCombobox
						.getSelectedItem().toString());
				doUpdateList();
				// Set lai text cho field Prediction
				predictionField.setText("");
				// Set lai cho combobox type Prediction
				typePCombobox.setSelectedIndex(0);
				JOptionPane.showMessageDialog(null, "Predict successfully!!!");
				JOptionPane
						.showMessageDialog(null,
								"Please check your directory to view your Prediction file!!!");

			}
		});
		listPredictionPanel = new javax.swing.JPanel();
		listPredictionPanel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC,
				13));
		listPredictionPanel.setBackground(new Color(153, 153, 153));
		scrollListPrediction = new javax.swing.JScrollPane();
		listPrediction = new javax.swing.JList<String>();
		listPrediction.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mEvent) {
				// Lay noi dung cua Prediction tuong ung voi index
				String result = statisticSH.getPredictionContent(listPrediction
						.getSelectedValue());
				// Thiet lap noi dung cho text area
				predictionReportField.setText(result);
			}
		});
		scrollPredictionField = new javax.swing.JScrollPane();
		predictionReportField = new javax.swing.JTextArea();
		predictionReportField.setLineWrap(true);
		predictionReportField.setWrapStyleWord(true);
		predictionReportField.setEditable(false);
		predictionReportField.setFont(new Font("Arial Black", Font.BOLD
				| Font.ITALIC, 13));

		statisticPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Statistic"));

		chooseTypeSLabel.setText("Choose type");

		exportTypeSlabel.setText("Export type");

		statisticButton.setText("Statistic");

		listStatisticPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("List statistic"));

		scrollListStatistic.setViewportView(listStatistic);

		statisticReportField.setColumns(20);
		statisticReportField.setRows(5);
		scrollStatisticField.setViewportView(statisticReportField);

		javax.swing.GroupLayout listStatisticPanelLayout = new javax.swing.GroupLayout(
				listStatisticPanel);
		listStatisticPanelLayout.setHorizontalGroup(listStatisticPanelLayout
				.createParallelGroup(Alignment.LEADING).addGroup(
						listStatisticPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(scrollListStatistic,
										GroupLayout.PREFERRED_SIZE, 83,
										GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addComponent(scrollStatisticField,
										GroupLayout.DEFAULT_SIZE, 182,
										Short.MAX_VALUE).addContainerGap()));
		listStatisticPanelLayout
				.setVerticalGroup(listStatisticPanelLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								listStatisticPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												listStatisticPanelLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																scrollStatisticField,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																161,
																Short.MAX_VALUE)
														.addComponent(
																scrollListStatistic,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																161,
																Short.MAX_VALUE))
										.addContainerGap()));
		listStatisticPanel.setLayout(listStatisticPanelLayout);

		predictionPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Prediction"));

		predictionField.setColumns(20);
		predictionField.setRows(5);
		scrollPrediction.setViewportView(predictionField);
		predictButton.setText("predict");

		listPredictionPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("List prediction"));

		scrollListPrediction.setViewportView(listPrediction);

		predictionReportField.setColumns(20);
		predictionReportField.setRows(5);
		scrollPredictionField.setViewportView(predictionReportField);

		javax.swing.GroupLayout listPredictionPanelLayout = new javax.swing.GroupLayout(
				listPredictionPanel);
		listPredictionPanelLayout.setHorizontalGroup(listPredictionPanelLayout
				.createParallelGroup(Alignment.LEADING).addGroup(
						Alignment.TRAILING,
						listPredictionPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(scrollListPrediction,
										GroupLayout.PREFERRED_SIZE, 81,
										GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addComponent(scrollPredictionField,
										GroupLayout.DEFAULT_SIZE, 170,
										Short.MAX_VALUE).addContainerGap()));
		listPredictionPanelLayout
				.setVerticalGroup(listPredictionPanelLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								listPredictionPanelLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												listPredictionPanelLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																scrollListPrediction,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																144,
																Short.MAX_VALUE)
														.addComponent(
																scrollPredictionField,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																144,
																Short.MAX_VALUE))
										.addContainerGap()));
		listPredictionPanel.setLayout(listPredictionPanelLayout);
		GroupLayout gl_predictionPanel = new GroupLayout(predictionPanel);
		gl_predictionPanel.setHorizontalGroup(gl_predictionPanel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_predictionPanel
								.createSequentialGroup()
								.addGap(28)
								.addComponent(typePCombobox, 0, 137,
										Short.MAX_VALUE)
								.addGap(18)
								.addComponent(predictButton,
										GroupLayout.DEFAULT_SIZE, 116,
										Short.MAX_VALUE).addGap(31))
				.addGroup(
						gl_predictionPanel
								.createSequentialGroup()
								.addGap(32)
								.addComponent(scrollPrediction,
										GroupLayout.DEFAULT_SIZE, 267,
										Short.MAX_VALUE).addGap(31))
				.addGroup(
						gl_predictionPanel
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(listPredictionPanel,
										GroupLayout.DEFAULT_SIZE, 301,
										Short.MAX_VALUE).addGap(19)));
		gl_predictionPanel
				.setVerticalGroup(gl_predictionPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_predictionPanel
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(scrollPrediction,
												GroupLayout.PREFERRED_SIZE, 88,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_predictionPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																typePCombobox,
																GroupLayout.PREFERRED_SIZE,
																36,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																predictButton,
																GroupLayout.PREFERRED_SIZE,
																36,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addComponent(listPredictionPanel,
												GroupLayout.DEFAULT_SIZE, 204,
												Short.MAX_VALUE)
										.addContainerGap()));
		predictionPanel.setLayout(gl_predictionPanel);
		GroupLayout gl_statisticPanel = new GroupLayout(statisticPanel);
		gl_statisticPanel
				.setHorizontalGroup(gl_statisticPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_statisticPanel
										.createSequentialGroup()
										.addGroup(
												gl_statisticPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_statisticPanel
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				listStatisticPanel,
																				GroupLayout.DEFAULT_SIZE,
																				327,
																				Short.MAX_VALUE))
														.addGroup(
																gl_statisticPanel
																		.createSequentialGroup()
																		.addGap(38)
																		.addGroup(
																				gl_statisticPanel
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								statisticButton,
																								GroupLayout.DEFAULT_SIZE,
																								265,
																								Short.MAX_VALUE)
																						.addGroup(
																								gl_statisticPanel
																										.createSequentialGroup()
																										.addGroup(
																												gl_statisticPanel
																														.createParallelGroup(
																																Alignment.TRAILING)
																														.addComponent(
																																chooseTypeSLabel,
																																Alignment.LEADING,
																																GroupLayout.DEFAULT_SIZE,
																																118,
																																Short.MAX_VALUE)
																														.addComponent(
																																exportTypeSlabel,
																																GroupLayout.DEFAULT_SIZE,
																																118,
																																Short.MAX_VALUE))
																										.addGap(28)
																										.addGroup(
																												gl_statisticPanel
																														.createParallelGroup(
																																Alignment.LEADING,
																																false)
																														.addComponent(
																																typeSCombobox,
																																GroupLayout.PREFERRED_SIZE,
																																119,
																																GroupLayout.PREFERRED_SIZE)
																														.addComponent(
																																typeStatisticCombobox,
																																GroupLayout.PREFERRED_SIZE,
																																119,
																																GroupLayout.PREFERRED_SIZE))))
																		.addGap(34)))
										.addContainerGap()));
		gl_statisticPanel
				.setVerticalGroup(gl_statisticPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_statisticPanel
										.createSequentialGroup()
										.addGap(11)
										.addGroup(
												gl_statisticPanel
														.createParallelGroup(
																Alignment.BASELINE,
																false)
														.addGroup(
																gl_statisticPanel
																		.createSequentialGroup()
																		.addGap(2)
																		.addComponent(
																				typeStatisticCombobox,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(
																chooseTypeSLabel,
																GroupLayout.PREFERRED_SIZE,
																23,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_statisticPanel
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																exportTypeSlabel,
																GroupLayout.PREFERRED_SIZE,
																24,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																typeSCombobox,
																GroupLayout.PREFERRED_SIZE,
																24,
																GroupLayout.PREFERRED_SIZE))
										.addGap(27)
										.addComponent(statisticButton,
												GroupLayout.PREFERRED_SIZE, 36,
												GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(listStatisticPanel,
												GroupLayout.DEFAULT_SIZE, 206,
												Short.MAX_VALUE)
										.addContainerGap()));
		statisticPanel.setLayout(gl_statisticPanel);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.TRAILING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addGap(27)
						.addComponent(statisticPanel, GroupLayout.DEFAULT_SIZE,
								347, Short.MAX_VALUE)
						.addGap(18)
						.addComponent(predictionPanel,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGap(24)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								groupLayout
										.createSequentialGroup()
										.addGap(25)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																predictionPanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																397,
																Short.MAX_VALUE)
														.addComponent(
																statisticPanel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		getContentPane().setLayout(groupLayout);
		dlmStatistic = new DefaultListModel<String>();
		dlmPrediction = new DefaultListModel<String>();
		listStatistic.setModel(dlmStatistic);
		listPrediction.setModel(dlmPrediction);
		pack();

		statisticSH = new StatisticStationHandling(statisticStationID);
		this.validate();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new StatisticStationForm("SS01").setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel chooseTypeSLabel;
	private javax.swing.JLabel exportTypeSlabel;
	private javax.swing.JList<String> listPrediction;
	private javax.swing.JPanel listPredictionPanel;
	private javax.swing.JList<String> listStatistic;
	private javax.swing.JPanel listStatisticPanel;
	private javax.swing.JButton predictButton;
	private javax.swing.JTextArea predictionField;
	private javax.swing.JPanel predictionPanel;
	private javax.swing.JTextArea predictionReportField;
	private javax.swing.JScrollPane scrollListPrediction;
	private javax.swing.JScrollPane scrollListStatistic;
	private javax.swing.JScrollPane scrollPrediction;
	private javax.swing.JScrollPane scrollPredictionField;
	private javax.swing.JScrollPane scrollStatisticField;
	private javax.swing.JButton statisticButton;
	private javax.swing.JPanel statisticPanel;
	private javax.swing.JTextArea statisticReportField;
	private javax.swing.JComboBox<String> typePCombobox;
	private javax.swing.JComboBox<String> typeSCombobox;
	private javax.swing.JComboBox<String> typeStatisticCombobox;
	private DefaultListModel<String> dlmStatistic;
	private DefaultListModel<String> dlmPrediction;
	// End of variables declaration//GEN-END:variables

	private StatisticStationHandling statisticSH = null;

	public StatisticStationHandling getStatisticSH() {
		return statisticSH;
	}

	public void setStatisticSH(StatisticStationHandling statisticSH) {
		this.statisticSH = statisticSH;
	}

}
